<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>Document</title>
    <style>
      div {
        width: 100px;
        height: 100px;
        background-color: red;
        opacity: 1;
        margin-bottom: 100px;
      }
    </style>
  </head>
  <body>
    <div></div>
    <div></div>
    <div></div>
    <div></div>

    <script>
      const divEls = document.querySelectorAll("div");

      divEls.forEach((item) => {
        item.onmouseenter = function () {
          startMove(this, "width", 400);
        };

        item.onmouseleave = function () {
          startMove(this, "width", 100);
        };
      });

      function getStyle(dom, attr) {
        if (window.getComputedStyle) {
          return window.getComputedStyle(dom)[attr];
        } else {
          return dom.currentStyle[attr];
        }
      }

      function startMove(dom, attr, target, unit = "px") {
        clearInterval(dom.timer);
        let iSpeend = null;
        let curTarget = null;
        dom.timer = setInterval(() => {
          curTarget = parseFloat(getStyle(dom, attr));
          target = parseFloat(target);
          iSpeend = (target - curTarget) / 7;
          iSpeend = iSpeend > 0 ? Math.ceil(iSpeend) : Math.floor(iSpeend);
          if (curTarget === target) {
            clearInterval(dom.timer);
          } else {
            dom.style[attr] = curTarget + iSpeend + unit;
          }
        }, 30);
      }
    </script>
  </body>
</html>
